package com.ihteima.controller;

import com.ihteima.pojo.Category;
import com.ihteima.pojo.Response;
import com.ihteima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: CategoryController
 * Package: com.ihteima.controller
 * Description:
 *
 * @Auther gongkaiming
 * @Create 2025/10/26 21:23
 * @Version 1.0
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Response<?> add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return Response.success();
    }

    @GetMapping
    public Response<List<Category>> list(){
        List<Category> cs = categoryService.list();
        return Response.success(cs);
    }

    @GetMapping("/detail")
    public Response<Category> detail(Integer id){
        Category c = categoryService.findById(id);
        return Response.success(c);
    }

    @PutMapping
    public Response<?> update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return Response.success();
    }

    @DeleteMapping
    public Response<?> delete(Integer id){
        categoryService.deleteById(id);
        return Response.success();
    }
}
